﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel.SmartExcel;

namespace Office.Excel.Helper
{
    /// <summary>
    /// Only Export data to excel format file
    /// </summary>
    public class SmartExcelHelper
    {
        public SmartExcelHelper()
        {

        }

        
        SmartExcelBase _ExcelFile = new SmartExcelBase();

        List<ExcelCell> _Cells_List = new List<ExcelCell>();

        #region Public Methods
        public bool CreateFile(string filename)
        {
            try
            {


                _ExcelFile.CreateFile(filename);
                _ExcelFile.PrintGridLines = false;


                MarginTypes mt1 = MarginTypes.TopMargin;
                MarginTypes mt2 = MarginTypes.LeftMargin;
                MarginTypes mt3 = MarginTypes.RightMargin;
                MarginTypes mt4 = MarginTypes.BottomMargin;


                double height = 1.5;
                _ExcelFile.SetMargin(mt1, height);
                _ExcelFile.SetMargin(mt2, height);
                _ExcelFile.SetMargin(mt3, height);
                _ExcelFile.SetMargin(mt4, height);


                FontFormatting ff = FontFormatting.NoFormat;

                string font = "Arial";
                short fontsize = 12;
                _ExcelFile.SetFont(font, fontsize, ff);

                byte b1 = 1, b2 = 12;
                short s3 = 18;
                _ExcelFile.SetColumnWidth(b1, b2, s3);

                string header = "头";
                string footer = "角";
                _ExcelFile.SetHeader(header);
                _ExcelFile.SetFooter(footer);

                return (true);
            }
            catch (Exception err)
            {
                //throw exception
                return (false);
            }
        }
        public bool CloseFile()
        {
            if (_Cells_List.Count > 0)
            {
                SaveExcel();
            }
            int ret = _ExcelFile.CloseFile();
            if (ret != 0)
            {
                return (false);
            }
            else
            {
                return (true);
            }
        }

        public bool Add(ExcelCell cell)
        {
            try
            {
                _Cells_List.Add(cell);

                return (true);
            }
            catch (Exception err)
            {
                return (false);
            }

        } 
        #endregion


        #region Private Methods
        void SaveExcel()
        {
            foreach (ExcelCell cell in _Cells_List)
            {
                SaveCells(cell);
            }

            ///clear cell after save
            _Cells_List.Clear();
        }

        bool SaveCells(ExcelCell cell)
        {
            ValueTypes vt = ValueTypes.Text;
            CellFont cf = CellFont.Font0;
            CellAlignment ca = CellAlignment.LeftAlign;
            CellHiddenLocked chl = CellHiddenLocked.Normal;
            int cellformat = 0;

            try
            {
                _ExcelFile.WriteValue(vt, cf, ca, chl, cell.Row, cell.Col, cell.Text, cellformat);

                return (true);
            }
            catch (Exception err)
            {
                return (false);
            }
        }
        
        #endregion



    }

}
